package com.lc1993929.mr.flowlog.enhance;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;

/**
 * Created by LiuChang on 2018/1/8.
 */
public class FlowLogEnhance {

    static class FlowLogEnhanceMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
        HashMap<String, String> ruleMap = new HashMap<>();

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            DBLoader.load(ruleMap);
        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] fields = StringUtils.split(line, "\t");

           /* if (fields.length < 27) {
                return;
            }
            if (fields[26].startsWith("http")) {
                return;
            }*/

            //mapreduce提供的全局计数器工具
            Counter counter = context.getCounter("dataException", "line-malform");
            try {
                String url = fields[26];
                String content = ruleMap.get(url);
                if (!StringUtils.isEmpty(content)) {
                    context.write(new Text(line + "\t" + content), NullWritable.get());
                } else {
                    context.write(new Text(url + "\001tocrawl"), NullWritable.get());
                }
            } catch (Exception e) {
                counter.increment(1);
            }
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        //conf.set("mapreduce.framework.name", "local");
        Job job = Job.getInstance(conf);
        //要执行的jar文件
        job.setJarByClass(FlowLogEnhance.class);
        //指定mapper类和reducer类
        job.setMapperClass(FlowLogEnhanceMapper.class);
        //指定mapper和reducer的输出的key和value的数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);
        //指定输出规则
        //job.setOutputFormatClass(EnhanceOutputFormat.class);
        job.setOutputValueClass(SequenceFileOutputFormat.class);
        //指定要解析的文件路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        //指定输出的文件路径
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.setNumReduceTasks(0);
        // 向yarn提交job
        boolean res = job.waitForCompletion(true);

        System.exit(res ? 0 : 1);

    }
}
